package com.yun.zf.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.yun.zf.common.admin.domain.LoginUser;
import com.yun.zf.common.admin.domain.SysMenu;
import com.yun.zf.common.admin.model.sys.MenuAddMo;
import com.yun.zf.common.admin.model.sys.MenuEditMo;
import com.yun.zf.common.admin.model.sys.MenuSearchMo;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.utils.ServletUtils;
import com.yun.zf.common.vo.Response;
import com.yun.zf.system.security.service.TokenService;
import com.yun.zf.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/menu")
@Api(tags = "菜单管理")
public class SysMenuController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private TokenService tokenService;
    
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @PostMapping("/list")
    @ApiOperation(value = "菜单列表")
    public Response list(@RequestBody MenuSearchMo menu)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUserId();
        List<Tree<String>> menus = menuService.selectMenuList(menu, userId);
        return Response.success(menus);
    }

    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @PostMapping("/editMenuList")
    @ApiOperation(value = "菜单名称列表")
    public Response editMenuList(){
        List<Tree<String>> menus = menuService.menuList();

        return Response.success(menus);
    }


    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:menu:query')")
    @GetMapping(value = "/{menuId}")
    @ApiOperation(value = "菜单详情")
    public Response getInfo(@PathVariable Long menuId)
    {
        SysMenu menu =   menuService.selectMenuById(menuId);
        // 获取上级菜单名称
        if (menu!=null){
            SysMenu menuName =   menuService.selectMenuById(menu.getParentId());
            if (menuName!=null){
                menu.setParentName(menuName.getMenuName());
            }
    }
        return Response.success(menu);
    }

    
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @PostMapping("/add")
    @ApiOperation(value = "新增菜单")
    public Response add(@Validated @RequestBody MenuAddMo menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(-1L,menu.getMenuName())))
        {
            return Response.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        return   Response.success(menuService.insertMenu(menu));

    }


    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @PostMapping("/edit")
    @ApiOperation(value = "修改菜单")
    public Response edit(@Validated @RequestBody MenuEditMo menu)
    {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu.getMenuId(), menu.getMenuName())))
        {
            return Response.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        }
        return Response.success(menuService.updateMenu(menu));
    }
    
    @PreAuthorize("@ss.hasPermi('system:menu:remove')")
    @PostMapping("/remove")
    @ApiOperation(value = "删除菜单")
    public Response remove(@RequestBody Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            return Response.error("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId))
        {
            return Response.error("菜单已分配,不允许删除");
        }
        return Response.success(menuService.deleteMenuById(menuId));
    }
}